package com.pig4cloud.pig.bzg.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.bzg.entity.WzPhotoNewsEntity;
import com.pig4cloud.pig.bzg.service.WzPhotoNewsService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 图片新闻表
 *
 * @author pig
 * @date 2025-07-31 11:08:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wzPhotoNews" )
@Tag(description = "wzPhotoNews" , name = "图片新闻表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class WzPhotoNewsController {

    private final  WzPhotoNewsService wzPhotoNewsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wzPhotoNews 图片新闻表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('bzg_wzPhotoNews_view')" )
    public R getWzPhotoNewsPage(@ParameterObject Page page, @ParameterObject WzPhotoNewsEntity wzPhotoNews) {
        return R.ok(wzPhotoNewsService.selectWzPhotoNewsList(page, wzPhotoNews));
    }


    /**
     * 通过id查询图片新闻表
     * @param newsId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{newsId}" )
    @PreAuthorize("@pms.hasPermission('bzg_wzPhotoNews_view')" )
    public R getById(@PathVariable("newsId" ) Long newsId) {
        return R.ok(wzPhotoNewsService.getById(newsId));
    }

    /**
     * 新增图片新闻表
     * @param wzPhotoNews 图片新闻表
     * @return R
     */
    @Operation(summary = "新增图片新闻表" , description = "新增图片新闻表" )
    @SysLog("新增图片新闻表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bzg_wzPhotoNews_add')" )
    public R save(@RequestBody WzPhotoNewsEntity wzPhotoNews) {
        return R.ok(wzPhotoNewsService.save(wzPhotoNews));
    }

    /**
     * 修改图片新闻表
     * @param wzPhotoNews 图片新闻表
     * @return R
     */
    @Operation(summary = "修改图片新闻表" , description = "修改图片新闻表" )
    @SysLog("修改图片新闻表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bzg_wzPhotoNews_edit')" )
    public R updateById(@RequestBody WzPhotoNewsEntity wzPhotoNews) {
        return R.ok(wzPhotoNewsService.updateById(wzPhotoNews));
    }

    /**
     * 通过id删除图片新闻表
     * @param ids newsId列表
     * @return R
     */
    @Operation(summary = "通过id删除图片新闻表" , description = "通过id删除图片新闻表" )
    @SysLog("通过id删除图片新闻表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bzg_wzPhotoNews_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(wzPhotoNewsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param wzPhotoNews 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bzg_wzPhotoNews_export')" )
    public List<WzPhotoNewsEntity> export(WzPhotoNewsEntity wzPhotoNews,Long[] ids) {
        return wzPhotoNewsService.list(Wrappers.lambdaQuery(wzPhotoNews).in(ArrayUtil.isNotEmpty(ids), WzPhotoNewsEntity::getNewsId, ids));
    }
}